﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardInChina.Data.Entity.Entity;
using CardInChina.Data.Entity.Interface;
using CardInChina.Web.Mvc.Models;
using CardInChina.Common.Tool;

namespace CardInChina.Bussiness.Frontend.Interface
{
    public class CardInfoBusinessService
    {
        private static CardInfoService cis = new CardInfoService();
        private static DataCache cache = new DataCache();

        public IList<HotSearchItem> GetHotTopSearchItem(int top)
        {
            var topCardInfoes = cis.GetHotTopCardInfo(top);
            IList<HotSearchItem> ret = new List<HotSearchItem>();
            if (topCardInfoes == null) return ret;
            foreach (CardInfo c in topCardInfoes)
            {
                HotSearchItem hsi = new HotSearchItem();
                hsi.ItemID = c.CardKey;
                hsi.Display = c.CardName;
                hsi.Title = c.CardName;
                ret.Add(hsi);
            }
            return ret;
        }

        public IEnumerable<CardInfo> GetCardInfoesShowInHomePage()
        {
            return cache.GetData<IEnumerable<CardInfo>>("GetCardInfoesShowInHomePage", () =>
            {
                var topCardInfoes = cis.GetBestTopCardInfo(20, null);
                return topCardInfoes;
            });
        }

        public IEnumerable<CardInfo> GetCardInfoesShowByCategory(string category)
        {
            if (string.IsNullOrEmpty(category))
                throw new ArgumentNullException("category");

            return cis.GetBestTopCardInfo(20, category);
        }

        public IEnumerable<CardInfo> GetCardInfoesShowByGame(string gameKey)
        {
            if (string.IsNullOrEmpty(gameKey))
                throw new Exception("gameKey is null");
            else
                return cis.GetCardInfoByGame(gameKey);
        }


        public IEnumerable<CardInfo> Get10SaleHotTopCardInfo()
        {
            return cis.GetSaleHotTopCardInfo(10);
        }

        public IEnumerable<Card> GetCardsFromCardInfoID(int id)
        {
            var cardinfo = cis.GetCardInfoByIDWithCardLoaded(id);
            if (cardinfo == null) throw new Exception("cardinfo not found!");
            return cardinfo.OfCards.ToArray();
        }

        public CardInfo GetCardByKeyword(string cardKey)
        {
            if (string.IsNullOrEmpty(cardKey)) throw new Exception("cardinfo not found!");
            var cardinfo = cis.GetCardByKeywordWithCardLoaded(cardKey);
            cardinfo.FreeRatio = cardinfo.FreeRatio.Replace("\r", "<br/>");
            cardinfo.Description = cardinfo.Description.Replace("\r", "<br/>");
            if (cardinfo == null) throw new Exception("cardinfo not found!");
            return cardinfo;
        }

        public IEnumerable<Game> GetGamesOfCardInfo(CardInfo cardInfo)
        {
            if (cardInfo == null) throw new Exception("cardinfo could not be null!");
            return cis.GetGamesOfCardInfo(cardInfo.CardInfoID);
        }

        public IEnumerable<CardInfo> GetCardInfoesByKeyword(string Keyword)
        {
            if (string.IsNullOrEmpty(Keyword)) return Get10SaleHotTopCardInfo();
            return cis.GetCardInfoesByKeyword(Keyword);
        }

    }
}
